/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * bocciawii
 * Copyright (C) Grupo de trabajo ULE 2010 <ulewii@googlegroups.com>
 * 
 * teclado is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * teclado is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "elemento.h"


Elemento::Elemento(std::string id)
{
	_id = id;
	_image = 0;
	_spacing = 10;

	
	std::string _id;

	_posImg.x_inicio = 0;
	_posImg.y_inicio = 0;
	_posImg.x_final = 0;
	_posImg.y_final = 0;
	_posImg.alignment = 0;
	_posImg.ancho = 0;
	_posImg.alto = 0;
	
}


Elemento::~Elemento()
{
	glDeleteTextures(1, &_image);
}

void Elemento::SetImage(std::string nombre, int tam_x, int tam_y, int img_align)
{
	// No hay necesidad de darle la vuelta a la imagen
	//  ya ue ahora estamos en 2D, y nuestro sistema de coordenadas
	//  es idéntido al de SDL, así que guay :D
	SDL_Surface *imagen;
	GLenum formato_textura;

	imagen = LoadPNG(RUTA_IMAGENES + nombre);
    if (!imagen) 
	{
		//TODO gestión errore;
	}

	
	if (imagen->format->BytesPerPixel == 4)     // contains an alpha channel
	{
		if (imagen->format->Rmask == 0x000000ff)
			formato_textura = GL_RGBA;
		else
			formato_textura = GL_BGRA;

	} else if (imagen->format->BytesPerPixel == 3)     // no alpha channel
	{
		if (imagen->format->Rmask == 0x000000ff)
			formato_textura = GL_RGB;
		else
			formato_textura = GL_BGR;
	} else {
		printf("warning: the image is not truecolor..  this will probably break\n");
		// TODO Gestión errores
	}
	
    // Create Textures	   
	glGenTextures(1, &_image);
	glBindTexture(GL_TEXTURE_2D, _image);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

	gluBuild2DMipmaps(GL_TEXTURE_2D, 4, imagen->w, imagen->h,formato_textura, GL_UNSIGNED_BYTE, imagen->pixels);


	_posImg.ancho = tam_x;
	_posImg.alto = tam_y;
	
	if (tam_x == -1)
		_posImg.ancho = imagen->w;
	if (tam_x == -1)
		_posImg.alto = imagen->h;

	_posImg.alignment = img_align;
	

	SDL_FreeSurface(imagen);
}






void Elemento::SetImage(GLuint id_gl, int tam_x, int tam_y, int img_align)
{
	_image = id_gl;
	
	_posImg.ancho = tam_x;
	_posImg.alto = tam_y;
	
	_posImg.alignment = img_align;
}










/*****
 *
 *
 *
 *
 *
 *		BORRAR PROBABLEMENTE
 **
 *
 *
 *
 *
 *
 */

void Elemento::SetBGColor(float* color)
{
	std::cerr << _id << ": Elemento equivocado para SetBGColor()" << std::endl;
}

void Elemento::SetFGColor(float* color)
{
	std::cerr << _id << ": Elemento equivocado para SetFGColor()" << std::endl;
}

int* Elemento::GetNeededSpace()
{
	std::cerr << _id << ": Elemento equivocado para GetNeededSpace()" << std::endl;
	return new int(2);
}

int* Elemento::GetScreenCoords()
{
	std::cerr << _id << ": Elemento equivocado para GetScreenCoords()" << std::endl;
	return new int(2);
}


void Elemento::Pack(int centro_x, int centro_y)
{
	std::cerr << _id << ": Elemento equivocado para Pack()" << std::endl;
}

void Elemento::Dibujar()
{
	std::cerr << _id << ": Elemento equivocado para Dibujar()" << std::endl;
}


void Elemento::SetText(std::string text)	
{
	std::cerr << _id << ": Elemento equivocado para SetText()" << std::endl;
}
void Elemento::SetText(int text)	
{
	std::cerr << _id << ": Elemento equivocado para SetText()" << std::endl;
}
void Elemento::SetText(float text)	
{
	std::cerr << _id << ": Elemento equivocado para SetText()" << std::endl;
}


void Elemento::SetTextAlign(FTGL::TextAlignment align, int line_lenght)
{
	std::cerr << _id << ": Elemento equivocado para SetTextAlign()" << std::endl;
}


void Elemento::SetSize(int size)
{
	std::cerr << _id << ": Elemento equivocado para SetTextSize()" << std::endl;
}


void Elemento::SetPercent(int percent)
{
	std::cerr << _id << ": Elemento equivocado para SetPercent()" << std::endl;
}













SDL_Surface* Elemento::LoadPNG (std::string ruta){
    Uint8 *rowhi, *rowlo;
    Uint8 *tmpbuf, tmpch;
    SDL_Surface *image;
    int i, j;

	SDL_RWops *rwop;
	rwop = SDL_RWFromFile(ruta.c_str(), "rb");
	image = IMG_LoadPNG_RW(rwop);

    if ( image == NULL ) {
		//TODO gestión de erroes
	}

	
    return(image);
}

